﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task.Entity.Config
{
    /// <summary>
    ///任务列表
    /// </summary>
    public class TaskRoot
    {
        private List<HandlerConfig> Handler = null;
        public TaskRoot()
        {
            Handler = new List<HandlerConfig>();
        }
        public void Add(HandlerConfig handler)
        {
            Handler.Add(handler);
        }

        public void Add(List<HandlerConfig> handlerList)
        {
            foreach (var handler in handlerList)
            {
                if (Exist(handler))
                {
                    Handler.Remove(handler);
                }
                Handler.Add(handler);
            }
        }
        public void Remove(HandlerConfig handler)
        {
            if (Exist(handler))
            {
                Handler.Remove(handler);
            }
        }
        public bool Exist()
        {
            return this.Count() > 0;
        }
        public bool Exist(HandlerConfig handler)
        {
            return Handler.Exists(t => t.Assembly == handler.Assembly &&
                t.Type == handler.Type &&
                t.WithCronSchedule == handler.WithCronSchedule);
        }
        public void RemoveAll()
        {
            foreach (var handler in Handler)
            {
                Handler.Remove(handler);
            }
        }
        public int Count()
        {
            return Handler.Count();
        }
        public List<HandlerConfig> List()
        {
            return Handler;
        }
    }
}
